{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-18T14:31:27.305394Z",
     "start_time": "2025-08-18T14:31:26.110727Z"
    }
   },
   "source": [
    "from qdrant_client.http import model\n",
    "\n",
    "# 1. 创建一些示例\n",
    "samples = [\n",
    "\n",
    "  {\n",
    "    \"flower_type\": \"玫瑰\",\n",
    "    \"occasion\": \"爱情\",\n",
    "    \"ad_copy\": \"玫瑰，浪漫的象征，是你向心爱的人表达爱意的最佳选择。\"\n",
    "  },\n",
    "  {\n",
    "    \"flower_type\": \"康乃馨\",\n",
    "    \"occasion\": \"母亲节\",\n",
    "    \"ad_copy\": \"康乃馨代表着母爱的纯洁与伟大，是母亲节赠送给母亲的完美礼物。\"\n",
    "  },\n",
    "  {\n",
    "    \"flower_type\": \"百合\",\n",
    "    \"occasion\": \"庆祝\",\n",
    "    \"ad_copy\": \"百合象征着纯洁与高雅，是你庆祝特殊时刻的理想选择。\"\n",
    "  },\n",
    "  {\n",
    "    \"flower_type\": \"向日葵\",\n",
    "    \"occasion\": \"鼓励\",\n",
    "    \"ad_copy\": \"向日葵象征着坚韧和乐观，是你鼓励亲朋好友的最好方式。\"\n",
    "  }\n",
    "]\n",
    "\n",
    "# 2. 创建一个提示模板\n",
    "from langchain.prompts.prompt import PromptTemplate\n",
    "prompt_sample = PromptTemplate(input_variables=[\"flower_type\", \"occasion\", \"ad_copy\"], \n",
    "                               template=\"鲜花类型: {flower_type}\\n场合: {occasion}\\n文案: {ad_copy}\")\n",
    "print(prompt_sample.format(**samples[0]))\n",
    "\n",
    "# 3. 创建一个FewShotPromptTemplate对象\n",
    "from langchain.prompts.few_shot import FewShotPromptTemplate\n",
    "prompt = FewShotPromptTemplate(\n",
    "    examples=samples,\n",
    "    example_prompt=prompt_sample,\n",
    "    suffix=\"鲜花类型: {flower_type}\\n场合: {occasion}\",\n",
    "    input_variables=[\"flower_type\", \"occasion\"]\n",
    ")\n",
    "print(prompt.format(flower_type=\"野玫瑰\", occasion=\"爱情\"))\n",
    "\n",
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "chatLLM = ChatOpenAI(\n",
    "    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key=\"sk-xxx\",\n",
    "    api_key=os.getenv(\"DASH_SCOPE_API_KEY\"), # 如何获取API Key：https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key\n",
    "    base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    model=\"qwen-plus\",\n",
    "    temperature=0.8,\n",
    "    max_tokens=60,\n",
    ")\n",
    "\n",
    "result = chatLLM.invoke(prompt.format(flower_type=\"野玫瑰\", occasion=\"爱情\"))\n",
    "print(result.content)\n",
    "\n",
    "\n",
    "# 5. 使用示例选择器\n",
    "from langchain.prompts.example_selector import SemanticSimilarityExampleSelector\n",
    "from langchain.vectorstores import Qdrant\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "\n",
    "# 初始化示例选择器\n",
    "example_selector = SemanticSimilarityExampleSelector.from_examples(\n",
    "    samples,\n",
    "    OpenAIEmbeddings(),\n",
    "    Qdrant,\n",
    "    k=1\n",
    ")\n",
    "\n",
    "# 创建一个使用示例选择器的FewShotPromptTemplate对象\n",
    "prompt = FewShotPromptTemplate(\n",
    "    example_selector=example_selector, \n",
    "    example_prompt=prompt_sample, \n",
    "    suffix=\"鲜花类型: {flower_type}\\n场合: {occasion}\", \n",
    "    input_variables=[\"flower_type\", \"occasion\"]\n",
    ")\n",
    "print(prompt.format(flower_type=\"红玫瑰\", occasion=\"爱情\"))\n",
    "\n",
    "result = chatLLM.invoke(prompt.format(flower_type=\"红玫瑰\", occasion=\"爱情\"))\n",
    "print(result.content)\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "鲜花类型: 玫瑰\n",
      "场合: 爱情\n",
      "文案: 玫瑰，浪漫的象征，是你向心爱的人表达爱意的最佳选择。\n",
      "鲜花类型: 玫瑰\n",
      "场合: 爱情\n",
      "文案: 玫瑰，浪漫的象征，是你向心爱的人表达爱意的最佳选择。\n",
      "\n",
      "鲜花类型: 康乃馨\n",
      "场合: 母亲节\n",
      "文案: 康乃馨代表着母爱的纯洁与伟大，是母亲节赠送给母亲的完美礼物。\n",
      "\n",
      "鲜花类型: 百合\n",
      "场合: 庆祝\n",
      "文案: 百合象征着纯洁与高雅，是你庆祝特殊时刻的理想选择。\n",
      "\n",
      "鲜花类型: 向日葵\n",
      "场合: 鼓励\n",
      "文案: 向日葵象征着坚韧和乐观，是你鼓励亲朋好友的最好方式。\n",
      "\n",
      "鲜花类型: 野玫瑰\n",
      "场合: 爱情\n",
      "鲜花类型: 野玫瑰  \n",
      "场合: 爱情  \n",
      "文案: 野玫瑰带着自然的纯粹与热烈，是表达真挚爱意的完美象征，让你的爱情如花般绽放。\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'ChatOpenAI' object has no attribute 'embed_documents'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[10]\u001B[39m\u001B[32m, line 65\u001B[39m\n\u001B[32m     62\u001B[39m \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34;01mlangchain\u001B[39;00m\u001B[34;01m.\u001B[39;00m\u001B[34;01membeddings\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mimport\u001B[39;00m OpenAIEmbeddings\n\u001B[32m     64\u001B[39m \u001B[38;5;66;03m# 初始化示例选择器\u001B[39;00m\n\u001B[32m---> \u001B[39m\u001B[32m65\u001B[39m example_selector = \u001B[43mSemanticSimilarityExampleSelector\u001B[49m\u001B[43m.\u001B[49m\u001B[43mfrom_examples\u001B[49m\u001B[43m(\u001B[49m\n\u001B[32m     66\u001B[39m \u001B[43m    \u001B[49m\u001B[43msamples\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m     67\u001B[39m \u001B[43m    \u001B[49m\u001B[43mchatLLM\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m     68\u001B[39m \u001B[43m    \u001B[49m\u001B[43mQdrant\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m     69\u001B[39m \u001B[43m    \u001B[49m\u001B[43mk\u001B[49m\u001B[43m=\u001B[49m\u001B[32;43m1\u001B[39;49m\n\u001B[32m     70\u001B[39m \u001B[43m)\u001B[49m\n\u001B[32m     72\u001B[39m \u001B[38;5;66;03m# 创建一个使用示例选择器的FewShotPromptTemplate对象\u001B[39;00m\n\u001B[32m     73\u001B[39m prompt = FewShotPromptTemplate(\n\u001B[32m     74\u001B[39m     example_selector=example_selector, \n\u001B[32m     75\u001B[39m     example_prompt=prompt_sample, \n\u001B[32m     76\u001B[39m     suffix=\u001B[33m\"\u001B[39m\u001B[33m鲜花类型: \u001B[39m\u001B[38;5;132;01m{flower_type}\u001B[39;00m\u001B[38;5;130;01m\\n\u001B[39;00m\u001B[33m场合: \u001B[39m\u001B[38;5;132;01m{occasion}\u001B[39;00m\u001B[33m\"\u001B[39m, \n\u001B[32m     77\u001B[39m     input_variables=[\u001B[33m\"\u001B[39m\u001B[33mflower_type\u001B[39m\u001B[33m\"\u001B[39m, \u001B[33m\"\u001B[39m\u001B[33moccasion\u001B[39m\u001B[33m\"\u001B[39m]\n\u001B[32m     78\u001B[39m )\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\python-workspace\\python_analyse\\.venv\\Lib\\site-packages\\langchain_core\\example_selectors\\semantic_similarity.py:171\u001B[39m, in \u001B[36mSemanticSimilarityExampleSelector.from_examples\u001B[39m\u001B[34m(cls, examples, embeddings, vectorstore_cls, k, input_keys, example_keys, vectorstore_kwargs, **vectorstore_cls_kwargs)\u001B[39m\n\u001B[32m    151\u001B[39m \u001B[38;5;250m\u001B[39m\u001B[33;03m\"\"\"Create k-shot example selector using example list and embeddings.\u001B[39;00m\n\u001B[32m    152\u001B[39m \n\u001B[32m    153\u001B[39m \u001B[33;03mReshuffles examples dynamically based on query similarity.\u001B[39;00m\n\u001B[32m   (...)\u001B[39m\u001B[32m    168\u001B[39m \u001B[33;03m    The ExampleSelector instantiated, backed by a vector store.\u001B[39;00m\n\u001B[32m    169\u001B[39m \u001B[33;03m\"\"\"\u001B[39;00m\n\u001B[32m    170\u001B[39m string_examples = [\u001B[38;5;28mcls\u001B[39m._example_to_text(eg, input_keys) \u001B[38;5;28;01mfor\u001B[39;00m eg \u001B[38;5;129;01min\u001B[39;00m examples]\n\u001B[32m--> \u001B[39m\u001B[32m171\u001B[39m vectorstore = \u001B[43mvectorstore_cls\u001B[49m\u001B[43m.\u001B[49m\u001B[43mfrom_texts\u001B[49m\u001B[43m(\u001B[49m\n\u001B[32m    172\u001B[39m \u001B[43m    \u001B[49m\u001B[43mstring_examples\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43membeddings\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmetadatas\u001B[49m\u001B[43m=\u001B[49m\u001B[43mexamples\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43m*\u001B[49m\u001B[43m*\u001B[49m\u001B[43mvectorstore_cls_kwargs\u001B[49m\n\u001B[32m    173\u001B[39m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n\u001B[32m    174\u001B[39m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mcls\u001B[39m(\n\u001B[32m    175\u001B[39m     vectorstore=vectorstore,\n\u001B[32m    176\u001B[39m     k=k,\n\u001B[32m   (...)\u001B[39m\u001B[32m    179\u001B[39m     vectorstore_kwargs=vectorstore_kwargs,\n\u001B[32m    180\u001B[39m )\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\python-workspace\\python_analyse\\.venv\\Lib\\site-packages\\langchain_community\\vectorstores\\qdrant.py:1337\u001B[39m, in \u001B[36mQdrant.from_texts\u001B[39m\u001B[34m(cls, texts, embedding, metadatas, ids, location, url, port, grpc_port, prefer_grpc, https, api_key, prefix, timeout, host, path, collection_name, distance_func, content_payload_key, metadata_payload_key, vector_name, batch_size, shard_number, replication_factor, write_consistency_factor, on_disk_payload, hnsw_config, optimizers_config, wal_config, quantization_config, init_from, on_disk, force_recreate, **kwargs)\u001B[39m\n\u001B[32m   1197\u001B[39m \u001B[38;5;129m@classmethod\u001B[39m\n\u001B[32m   1198\u001B[39m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34mfrom_texts\u001B[39m(\n\u001B[32m   1199\u001B[39m     \u001B[38;5;28mcls\u001B[39m: Type[Qdrant],\n\u001B[32m   (...)\u001B[39m\u001B[32m   1232\u001B[39m     **kwargs: Any,\n\u001B[32m   1233\u001B[39m ) -> Qdrant:\n\u001B[32m   1234\u001B[39m \u001B[38;5;250m    \u001B[39m\u001B[33;03m\"\"\"Construct Qdrant wrapper from a list of texts.\u001B[39;00m\n\u001B[32m   1235\u001B[39m \n\u001B[32m   1236\u001B[39m \u001B[33;03m    Args:\u001B[39;00m\n\u001B[32m   (...)\u001B[39m\u001B[32m   1335\u001B[39m \u001B[33;03m            qdrant = Qdrant.from_texts(texts, embeddings, \"localhost\")\u001B[39;00m\n\u001B[32m   1336\u001B[39m \u001B[33;03m    \"\"\"\u001B[39;00m\n\u001B[32m-> \u001B[39m\u001B[32m1337\u001B[39m     qdrant = \u001B[38;5;28;43mcls\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43mconstruct_instance\u001B[49m\u001B[43m(\u001B[49m\n\u001B[32m   1338\u001B[39m \u001B[43m        \u001B[49m\u001B[43mtexts\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1339\u001B[39m \u001B[43m        \u001B[49m\u001B[43membedding\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1340\u001B[39m \u001B[43m        \u001B[49m\u001B[43mlocation\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1341\u001B[39m \u001B[43m        \u001B[49m\u001B[43murl\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1342\u001B[39m \u001B[43m        \u001B[49m\u001B[43mport\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1343\u001B[39m \u001B[43m        \u001B[49m\u001B[43mgrpc_port\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1344\u001B[39m \u001B[43m        \u001B[49m\u001B[43mprefer_grpc\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1345\u001B[39m \u001B[43m        \u001B[49m\u001B[43mhttps\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1346\u001B[39m \u001B[43m        \u001B[49m\u001B[43mapi_key\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1347\u001B[39m \u001B[43m        \u001B[49m\u001B[43mprefix\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1348\u001B[39m \u001B[43m        \u001B[49m\u001B[43mtimeout\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1349\u001B[39m \u001B[43m        \u001B[49m\u001B[43mhost\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1350\u001B[39m \u001B[43m        \u001B[49m\u001B[43mpath\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1351\u001B[39m \u001B[43m        \u001B[49m\u001B[43mcollection_name\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1352\u001B[39m \u001B[43m        \u001B[49m\u001B[43mdistance_func\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1353\u001B[39m \u001B[43m        \u001B[49m\u001B[43mcontent_payload_key\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1354\u001B[39m \u001B[43m        \u001B[49m\u001B[43mmetadata_payload_key\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1355\u001B[39m \u001B[43m        \u001B[49m\u001B[43mvector_name\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1356\u001B[39m \u001B[43m        \u001B[49m\u001B[43mshard_number\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1357\u001B[39m \u001B[43m        \u001B[49m\u001B[43mreplication_factor\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1358\u001B[39m \u001B[43m        \u001B[49m\u001B[43mwrite_consistency_factor\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1359\u001B[39m \u001B[43m        \u001B[49m\u001B[43mon_disk_payload\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1360\u001B[39m \u001B[43m        \u001B[49m\u001B[43mhnsw_config\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1361\u001B[39m \u001B[43m        \u001B[49m\u001B[43moptimizers_config\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1362\u001B[39m \u001B[43m        \u001B[49m\u001B[43mwal_config\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1363\u001B[39m \u001B[43m        \u001B[49m\u001B[43mquantization_config\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1364\u001B[39m \u001B[43m        \u001B[49m\u001B[43minit_from\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1365\u001B[39m \u001B[43m        \u001B[49m\u001B[43mon_disk\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1366\u001B[39m \u001B[43m        \u001B[49m\u001B[43mforce_recreate\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1367\u001B[39m \u001B[43m        \u001B[49m\u001B[43m*\u001B[49m\u001B[43m*\u001B[49m\u001B[43mkwargs\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m   1368\u001B[39m \u001B[43m    \u001B[49m\u001B[43m)\u001B[49m\n\u001B[32m   1369\u001B[39m     qdrant.add_texts(texts, metadatas, ids, batch_size)\n\u001B[32m   1370\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m qdrant\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\python-workspace\\python_analyse\\.venv\\Lib\\site-packages\\langchain_community\\vectorstores\\qdrant.py:1639\u001B[39m, in \u001B[36mQdrant.construct_instance\u001B[39m\u001B[34m(cls, texts, embedding, location, url, port, grpc_port, prefer_grpc, https, api_key, prefix, timeout, host, path, collection_name, distance_func, content_payload_key, metadata_payload_key, vector_name, shard_number, replication_factor, write_consistency_factor, on_disk_payload, hnsw_config, optimizers_config, wal_config, quantization_config, init_from, on_disk, force_recreate, **kwargs)\u001B[39m\n\u001B[32m   1636\u001B[39m \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34;01mqdrant_client\u001B[39;00m\u001B[34;01m.\u001B[39;00m\u001B[34;01mhttp\u001B[39;00m\u001B[34;01m.\u001B[39;00m\u001B[34;01mexceptions\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mimport\u001B[39;00m UnexpectedResponse\n\u001B[32m   1638\u001B[39m \u001B[38;5;66;03m# Just do a single quick embedding to get vector size\u001B[39;00m\n\u001B[32m-> \u001B[39m\u001B[32m1639\u001B[39m partial_embeddings = \u001B[43membedding\u001B[49m\u001B[43m.\u001B[49m\u001B[43membed_documents\u001B[49m(texts[:\u001B[32m1\u001B[39m])\n\u001B[32m   1640\u001B[39m vector_size = \u001B[38;5;28mlen\u001B[39m(partial_embeddings[\u001B[32m0\u001B[39m])\n\u001B[32m   1641\u001B[39m collection_name = collection_name \u001B[38;5;129;01mor\u001B[39;00m uuid.uuid4().hex\n",
      "\u001B[36mFile \u001B[39m\u001B[32mE:\\python-workspace\\python_analyse\\.venv\\Lib\\site-packages\\pydantic\\main.py:991\u001B[39m, in \u001B[36mBaseModel.__getattr__\u001B[39m\u001B[34m(self, item)\u001B[39m\n\u001B[32m    988\u001B[39m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28msuper\u001B[39m().\u001B[34m__getattribute__\u001B[39m(item)  \u001B[38;5;66;03m# Raises AttributeError if appropriate\u001B[39;00m\n\u001B[32m    989\u001B[39m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[32m    990\u001B[39m     \u001B[38;5;66;03m# this is the current error\u001B[39;00m\n\u001B[32m--> \u001B[39m\u001B[32m991\u001B[39m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m(\u001B[33mf\u001B[39m\u001B[33m'\u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mtype\u001B[39m(\u001B[38;5;28mself\u001B[39m).\u001B[34m__name__\u001B[39m\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m object has no attribute \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mitem\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[33m'\u001B[39m)\n",
      "\u001B[31mAttributeError\u001B[39m: 'ChatOpenAI' object has no attribute 'embed_documents'"
     ]
    }
   ],
   "execution_count": 10
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
